﻿/*
 * 页面描述：开关设置。如报价开关，解密开关，议价开关，竞价开关。
 * 创建时间：2009/8/25
 * 创建人：周司徒
 * 
 * 修改人：
 * 修改时间：
 * 修改内容：
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;
using System.Drawing;
using System.Text.RegularExpressions;
using HZ.Common;

public partial class Manage_SwitchSetting : System.Web.UI.Page, IAccess
{
    protected void Page_Load(object sender, EventArgs e)
    {

        CurrentAccount.CheckSessionTimeOut(this);
        if (!IsPostBack)
        {
            BindData();

        }
    }
    protected void btnbidswitch_Click(object sender, EventArgs e)
    {
        bool sw = (bool)ViewState["BidSwitch"];
        var pc = new ProjectConfig() { ProjectId = CurrentAccount.ProjectId };
        if (sw)
        {
            pc.BidSwitch = false;

        }
        else
        {
            if (SystemConfig.Instance.UseThreePwd == 1)
            {
                Response.Redirect("CheckThreePwd.aspx?action=bid");
                return;
            }
            else
            {
                pc.BidSwitch = true;
            }
        }

        BLL_Factory.IBLL_Manage.SetProjectConfig(pc);

        if (sw)
        {
            log4netManager.LogInfo("设置报价开关", string.Format("开启报价开关成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
        }
        else
        {
            log4netManager.LogInfo("设置报价开关", string.Format("关闭报价开关成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
        }
        BindData();
    }
    protected void btndecswitch_Click(object sender, EventArgs e)
    {
        bool sw = (bool)ViewState["DecSwitch"];
        var pc = new ProjectConfig() { ProjectId = CurrentAccount.ProjectId };
        if (sw)
        {
            pc.DecSwitch = false;
            // HZ.BidSystem.BLL.BLL_Service.Stop();
        }
        else
        {


            if (SystemConfig.Instance.UseThreePwd == 1)
            {
                Response.Redirect("CheckThreePwd.aspx?action=dec");
                return;
            }
            else
            {

                pc.DecSwitch = true;
            }
            // HZ.BidSystem.BLL.BLL_Service.ProjectId = CurrentAccount.ProjectId;
            // HZ.BidSystem.BLL.BLL_Service.Start();
        }

        BLL_Factory.IBLL_Manage.SetProjectConfig(pc);

        if (sw)
        {
            log4netManager.LogInfo("设置解密开关", string.Format("开启解密开关成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
        }
        else
        {
            log4netManager.LogInfo("设置解密开关", string.Format("关闭解密开关成功，操作人：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP), null);
        }
        BindData();

    }
    protected void btnTimeSet_Click(object sender, EventArgs e)
    {
        DateTime dt;

        //不能设置为空
        if (txtBidStartTime.Text.Trim() == "")
        {
            HZ.Web.WebUtil.Alert("报价开启时间不能为空！", this);
            return;
        }

        if (txtBidEndTime.Text.Trim() == "")
        {
            HZ.Web.WebUtil.Alert("报价截止时间不能为空！", this);
            return;
        }
        if (txtDecStartTime.Text.Trim() == "")
        {
            HZ.Web.WebUtil.Alert("报价解密开启时间不能为空！", this);
            return;
        }
        if (txtDecEndTime.Text.Trim() == "")
        {
            HZ.Web.WebUtil.Alert("报价解密截止时间不能为空！", this);
            return;
        }
        if (Convert.ToDateTime(txtBidStartTime.Text.Trim()) >= Convert.ToDateTime(txtBidEndTime.Text.Trim())) 
        {
            HZ.Web.WebUtil.Alert("报价开始时间不能大于报价结束时间！", this);
            return;
        }

        if (Convert.ToDateTime(txtDecStartTime.Text.Trim()) >= Convert.ToDateTime(txtDecEndTime.Text.Trim()))
        {
            HZ.Web.WebUtil.Alert("解密开始时间不能大于解密结束时间！", this);
            return;
        }

        if (Convert.ToDateTime(txtBidEndTime.Text.Trim()) >= Convert.ToDateTime(txtDecStartTime.Text.Trim()))
        {
            HZ.Web.WebUtil.Alert("报价结束时间不能大于解密开始时间！", this);
            return;
        }
        
        ProjectConfig pc = new ProjectConfig()
        {
            ProjectId = CurrentAccount.ProjectId,
            BidStartTime = new HZ.Common.ConvertHelper(txtBidStartTime.Text).ToDateTime,
            BidEndTime = new HZ.Common.ConvertHelper(txtBidEndTime.Text).ToDateTime,
            DecStartTime = new HZ.Common.ConvertHelper(txtDecStartTime.Text).ToDateTime,
            DecEndTime = new HZ.Common.ConvertHelper(txtDecEndTime.Text).ToDateTime
        };

        if (pc != null)
        {
            if (pc.BidStartTime == null)
            {
                HZ.Web.WebUtil.Alert("报价开启时间格式不正确！", this);
                return;
            }
            if (pc.BidEndTime == null)
            {
                HZ.Web.WebUtil.Alert("报价截止时间格式不正确！", this);
                return;
            }
            if (pc.DecStartTime == null)
            {
                HZ.Web.WebUtil.Alert("报价解密开启时间格式不正确！", this);
                return;
            }
            if (pc.DecEndTime == null)
            {
                HZ.Web.WebUtil.Alert("报价解密截止时间格式不正确！", this);
                return;
            }
            HZ.Web.WebUtil.Alert("设置完成！", this);
        }


        BLL_Factory.IBLL_Manage.SetProjectConfig(pc);

        log4netManager.LogInfo("设置报价时间", string.Format("设置报价时间成功，操作人：{0}，操作IP：{1}，操作信息：{2}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, HZ.Data.DAL_Helper.GetModelInfo(pc)), null);
    }

    #region 判断日期

    /// <summary>
/// 判断日期的日期部分格式
/// </summary>
/// <param name="dateStr">输入的日期的日期部分字符串</param>
/// <returns>bool</returns>
public bool isDateTime(string dateStr)
{
    bool _isDate = false;

    string matchStr = "";

    matchStr += @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$ ";

    RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));

    if (Regex.IsMatch(dateStr, matchStr, option))
        _isDate = true;
    else
        _isDate = false;

    return _isDate;

}

/// <summary>
/// 判断日期的时间部分格式
/// </summary>
/// <param name="time_str">输入日期的时间部分字符串</param>
/// <returns>bool</returns>
public bool isDateTime_time(string time_str)
{ 
    bool _isDate = false;

    string matchStr = "";

    //matchStr += @"^(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$ ";

    //matchStr += @"(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$";

    //matchStr += @"(0*[0-9]|[1-2][0-3]):(0*[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])";

    matchStr += @"^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$";

    RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));

    if (Regex.IsMatch(time_str, matchStr, option))
        _isDate = true;
    else
        _isDate = false;

    return _isDate;
}

/// <summary>
/// 判断日期的全部格式
/// </summary>
/// <param name="dateStr">输入日期的字符串</param>
/// <returns></returns>
public bool isDate(string dateStr)
{ 
    bool _isDate = false;

    string matchStr = "";

    matchStr += @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ";
    matchStr += @"(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$ ";

    RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));

    if (Regex.IsMatch(dateStr, matchStr, option))
        _isDate = true;
    else
        _isDate = false;

    return _isDate;
}

#endregion 判断日期


    protected void BindData()
    {
        var pc = BLL_Factory.IBLL_Manage.GetProjectConfig(CurrentAccount.ProjectId);
        ViewState["BidSwitch"] = pc.BidSwitch;
        ViewState["DecSwitch"] = pc.DecSwitch;
        if (pc.BidSwitch ?? false)
        {
            lblbidstatus.Text = "报价开关目前状态：开启";
            lblbidstatus.ForeColor = Color.Green;
            btnbidswitch.Text = "关闭报价开关";
        }
        else
        {
            lblbidstatus.Text = "报价开关目前状态：关闭";
            lblbidstatus.ForeColor = Color.Red;
            btnbidswitch.Text = "开启报价开关";
        }

        if (pc.DecSwitch ?? false)
        {
            lbldecstatus.Text = "解密开关目前状态：开启";
            lbldecstatus.ForeColor = Color.Green;
            btndecswitch.Text = "关闭解密开关";
        }
        else
        {
            lbldecstatus.Text = "解密开关目前状态：关闭";
            lbldecstatus.ForeColor = Color.Red;
            btndecswitch.Text = "开启解密开关";
        }

        txtBidStartTime.Text = pc.BidStartTime == null ? "" : pc.BidStartTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtBidEndTime.Text = pc.BidEndTime == null ? "" : pc.BidEndTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtDecStartTime.Text = pc.DecStartTime == null ? "" : pc.DecStartTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtDecEndTime.Text = pc.DecEndTime == null ? "" : pc.DecEndTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Manage);
            return list;
        }

    }

    #endregion
}
